<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="库房名称" prop="wareHouseId">
          <el-select clearable v-model="queryParams.wareHouseId"   placeholder="请选择库房名称">
            <el-option
              v-for="item in wareHouseoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="库房名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入库房名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="存储柜编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入存储柜编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存储柜名称" prop="cabinetName">
        <el-input
          v-model="queryParams.cabinetName"
          placeholder="请输入存储柜名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cabinet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cabinet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cabinet:remove']"
        >删除</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handlepd"
          v-hasPermi="['system:cabinet:edit']"
        >盘点</el-button>
      </el-col>  -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cabinetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="库房ID" align="center" prop="wareHouseId" /> -->
      <el-table-column label="库房名称" align="center" prop="warehouseName" />
      <el-table-column label="存储柜编号" align="center" prop="code" />
      <el-table-column label="存储柜名称" align="center" prop="cabinetName" />
      <!-- <el-table-column label="层数" align="center" prop="layer" /> -->
      <el-table-column label="格子编号" align="center" prop="boxNo" />
      <el-table-column label="ip地址" align="center" prop="cabinetIp" />
      
       <!--<el-table-column label="端口号" align="center" prop="cabinetPort" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cabinet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cabinet:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleopenhard(scope.row)"
            v-hasPermi="['system:cabinet:edit']"
          >固件升级</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改存储柜管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="库房名称" prop="wareHouseId">
        <el-select clearable v-model="form.wareHouseId" style="width:100%" placeholder="请选择库房名称" @change="handleChange">
            <el-option
              v-for="item in wareHouseoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="库房名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入库房名称" />
        </el-form-item> -->
        <el-form-item label="存储柜编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入存储柜编号" />
        </el-form-item>
        <el-form-item label="存储柜名称" prop="cabinetName">
          <el-input v-model="form.cabinetName" placeholder="请输入存储柜名称" />
        </el-form-item>
        <!-- <el-form-item label="层数" prop="layer">
          <el-input v-model="form.layer" placeholder="请输入层数" />
        </el-form-item> -->
        <el-form-item label="格子编号" prop="boxNo">
          <el-input v-model="form.boxNo" placeholder="请输入格子编号" />
        </el-form-item>
        <!-- <el-form-item label="ip地址" prop="cabinetIp">
          <el-input v-model="form.cabinetIp" placeholder="请输入ip地址" />
        </el-form-item> -->
        <!-- <el-form-item label="端口号" prop="cabinetPort">
          <el-input v-model="form.cabinetPort" placeholder="请输入端口号" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <el-dialog title="固件升级" :visible.sync="openhard" width="500px" append-to-body>
      <el-form ref="formhard" :model="formhard" :rules="ruleshard" label-width="100px">
        <el-form-item label="版本地址" prop="hardVersion">
          <el-input v-model="formhard.hardVersion" placeholder="请输入地址" />
        </el-form-item>
       </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormhard">确 定</el-button>
        <el-button @click="openhard=false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改存储柜管理对话框 -->
    <!-- <el-dialog title="盘点" :visible.sync="open" width="500px" append-to-body>
      <el-table v-loading="loading" :data="pdlist">
      <el-table-column label="标签" align="center" prop="id" />
      <el-table-column label="状态" align="center" prop="wareHouseId" /> 
    </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listCabinet, getCabinet, delCabinet, addCabinet, updateCabinet ,updateSingleHardVersion,rfid} from "@/api/archive/cabinet";
import {listwareHouse } from "@/api/archive/house";

export default {
  name: "Cabinet",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 存储柜管理表格数据
      cabinetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wareHouseId: null,
        warehouseName: null,
        code: null,
        cabinetName: null,
      },
      // 表单参数
      form: {},
      formhard:{},
      openhard:false,
      wareHouseoptions:[],
      cabinetoptions:[],
      openpd:false,
      pdlist:[],
      // 表单校验
      rules: {
        wareHouseId: [
          { required: true, message: "请选择库房", trigger: "change" }
        ],
        code: [
          { required: true, message: "请输入存储柜编号", trigger: "blur" }
        ],
        cabinetName: [
          { required: true, message: "请输入存储柜名称", trigger: "blur" }
        ],
        boxNo: [
          { required: true, message: "请输入存储格子编号", trigger: "blur" }
        ],
  
      },
      ruleshard:{
        hardVersion: [
          { required: true, message: "请输入版本地址", trigger: "blur" } 
        ]
      }
    };
  },
  created() {
    this.getList();
    this.wareHouse()
  },
  methods: {
    wareHouse() {
      listwareHouse().then(response => {
        this.wareHouseoptions = response.data;
      });
    },
    /** 查询存储柜管理列表 */
    getList() {
      this.loading = true;
      listCabinet(this.queryParams).then(response => {
        this.cabinetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wareHouseId: null,
        warehouseName: null,
        code: null,
        cabinetName: null,
        layer: null,
        boxNo: null,
        cabinetIp: null,
        cabinetPort: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
     handleChange(value) {
      // 根据选中的值查找对应的label
      const selectedOption = this.wareHouseoptions.find(item => item.id === value);
      if (selectedOption) {
       this.form.warehouseName = selectedOption.name;
      }
      console.log(selectedOption)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加存储柜管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCabinet(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改存储柜管理";
      });
    },
     /** 盘点 */
     handlepd(row) {
      const queypd={
       code:window.system.cabinetCode
      }
      rfid(queypd).then(response => {
        this.$modal.msgSuccess("盘点成功");
        // this.pdlist = response.data;
        // this.openpd = true;
 
      });
    },
    
       /** 固件升级钮操作 */
    handleopenhard(row) {
this.formhard = {
        id: null,
       hardVersion: null
      };
      this.formhard.id = row.id 
      this.openhard = true
    },
     /** 提交按钮 */
    submitFormhard() {
      this.$refs["formhard"].validate(valid => {
        if (valid) {
          if (this.formhard.id!= null) {
            updateSingleHardVersion(this.formhard).then(response => {
              this.$modal.msgSuccess("成功");
              this.openhard = false;
            }); 
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCabinet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCabinet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除存储柜管理编号为"' + ids + '"的数据项？').then(function() {
        return delCabinet(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cabinet/export', {
        ...this.queryParams
      }, `cabinet_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
